package com.ewzj.bigdata.mvp.ui.activity

import android.animation.IntEvaluator
import android.animation.ValueAnimator
import android.content.Context
import android.content.Intent
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.Window
import android.widget.LinearLayout
import cn.jzvd.JZVideoPlayer
import com.ewzj.bigdata.R
import com.ewzj.bigdata.app.config.AppKey
import com.ewzj.bigdata.app.extensions.dpToPx
import com.ewzj.bigdata.app.extensions.initwebview
import com.ewzj.bigdata.app.extensions.loadMiwoUrl
import com.ewzj.bigdata.app.utils.Apputils
import com.ewzj.bigdata.di.component.DaggerVideoWebActivityComponent
import com.ewzj.bigdata.di.module.VideoWebActivityModule
import com.ewzj.bigdata.mvp.contract.VideoWebviewActivityContract
import com.ewzj.bigdata.mvp.model.rspbean.VideoBean
import com.ewzj.bigdata.mvp.presenter.VideoWebActivityPresenter
import com.gyf.barlibrary.ImmersionBar
import com.jakewharton.rxbinding2.view.RxView
import com.jess.arms.base.BaseActivity
import com.jess.arms.di.component.AppComponent
import com.jess.arms.utils.DeviceUtils
import com.sina.weibo.sdk.utils.UIUtils
import com.umeng.socialize.bean.SHARE_MEDIA
import kotlinx.android.synthetic.main.activity_video_web.*
import kotlinx.android.synthetic.main.item_webview_comment.*
import kotlinx.android.synthetic.main.path_loading.*

class VideoWebActivity : BaseActivity<VideoWebActivityPresenter>(),VideoWebviewActivityContract.View {
    lateinit var  mImmersionBar:ImmersionBar
    override fun setCollectState() {
    }
    val shareWindow by lazy { Apputils.Utils.getShareWindow(llcoment) }
    lateinit var video:VideoBean
    override fun setHintText(content: String) {
        et_comment.setText("")
        et_comment.hint=content

    }

    override fun showComentBar() {
        runOnUiThread { showSerachBar(llcoment,0,54.dpToPx(this)) }
    }

    override fun initView(savedInstanceState: Bundle?): Int {
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        return R.layout.activity_video_web
    }

    override fun initData(savedInstanceState: Bundle?) {
        mImmersionBar=ImmersionBar.with(this)
        video=intent.getParcelableExtra<VideoBean>(AppKey.EXTRA_DATA)
        if(video==null)finish()
        tv_title.text=video.title
        iv_star.isSelected=video.is_collect==1
        initListener()
        bridgeview.initwebview(rl_pathloading)
        bridgeview.loadMiwoUrl(video.go_url)
        mPresenter.initJsBridge(bridgeview)
        mPresenter.initVideo(videoplayer,video)
        Apputils.Utils.setPathandStart(rl_pathloading)
    }

    private fun initListener() {
        RxView.clicks(iv_back).subscribe({finish()})
        RxView.clicks(tv_send).subscribe({
            if (Apputils.Utils.checkIsLogin(this)){
                mPresenter.doComment(et_comment.text.toString(),video)
            }else{
                LoginActivity.startLoginActivity(this)
            } })
        RxView.clicks(iv_star).subscribe({
            if(Apputils.Utils.checkIsLogin(this)){
                iv_star.isSelected=!iv_star.isSelected
                mPresenter.doCollect(video)
            }else{
                LoginActivity.startLoginActivity(this)
            }
        })
        RxView.clicks(tv_share).subscribe({
            shareWindow.showAtLocation(llcoment, Gravity.BOTTOM, 0, 0)
            Apputils.Utils.setbackgroudAlpa(this, 0.6.toFloat())})

        RxView.clicks(shareWindow.contentView.findViewById(R.id.ll_qq)).subscribe({
            mPresenter.ShareWeb(video, SHARE_MEDIA.QQ)
            shareWindow.dismiss()
        })
        RxView.clicks(shareWindow.contentView.findViewById(R.id.ll_qqzone)).subscribe({
            mPresenter.ShareWeb(video, SHARE_MEDIA.QZONE)
            shareWindow.dismiss()
        })
        RxView.clicks(shareWindow.contentView.findViewById(R.id.ll_wechat)).subscribe({
            mPresenter.ShareWeb(video, SHARE_MEDIA.WEIXIN)
            shareWindow.dismiss()
        })
        RxView.clicks(shareWindow.contentView.findViewById(R.id.ll_wechatzone)).subscribe({
            mPresenter.ShareWeb(video, SHARE_MEDIA.WEIXIN_CIRCLE)
            shareWindow.dismiss()
        })
        RxView.clicks(shareWindow.contentView.findViewById(R.id.ll_sina)).subscribe({
            mPresenter.ShareWeb(video, SHARE_MEDIA.SINA)
            shareWindow.dismiss()
        })
    }

    override fun setupActivityComponent(appComponent: AppComponent?) {
        DaggerVideoWebActivityComponent.builder().appComponent(appComponent)
                .videoWebActivityModule(VideoWebActivityModule(this))
                .build().inject(this)
    }

    override fun showLoading() {

    }

    override fun launchActivity(intent: Intent?) {

    }

    override fun hideLoading() {

    }

    override fun killMyself() {
    }

    override fun showMessage(message: String?) {
    }

    fun showSerachBar(view: View, startValue: Int, endValue: Int) {
        //      RelativeLayout.LayoutParams layoutParams= (RelativeLayout.LayoutParams) rlSearch.getLayoutParams();
        val valueAnimator = ValueAnimator.ofInt(0, 100)
        valueAnimator.duration = 200
        valueAnimator.addUpdateListener { animation ->
            val animatorValue = animation.animatedValue as Int
            val fraction = animatorValue / 100f
            val mEvaluator = IntEvaluator()
            //3.使用IntEvaluator计算属性值并赋值给ListView的高
            val layoutParams = view.layoutParams as LinearLayout.LayoutParams
            layoutParams.height = mEvaluator.evaluate(fraction, startValue, endValue)!!
            view.layoutParams = layoutParams
            view.requestLayout()
        }
        valueAnimator.setTarget(view)
        valueAnimator.start()
    }

    override fun onBackPressed() {
        mImmersionBar.reset().navigationBarEnable(false).statusBarDarkFont(true, 0.2f).statusBarColor(R.color.colorPrimary)
                .keyboardEnable(true)
                .fitsSystemWindows(true).init();
        if (JZVideoPlayer.backPress()) {
            return
        }
        super.onBackPressed()
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        if(newConfig.orientation == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
        {
            //TODO 某些操作
            DeviceUtils.setFullScreen(this)
        }else{
            DeviceUtils.cancelFullScreen(this)
        }
        super.onConfigurationChanged(newConfig)
    }
    override fun onPause() {
        JZVideoPlayer.releaseAllVideos()
        super.onPause()
    }
    companion object{
        fun startVideoWebView(context: Context, videoBean: VideoBean) {
            val intent = Intent(context, VideoWebActivity::class.java)
            intent.putExtra(AppKey.EXTRA_DATA, videoBean)
            context.startActivity(intent)
        }

    }

    override fun refreshVideo(video1: VideoBean) {
        video=video1
        tv_title.text=video.title
        iv_star.isSelected=video.is_collect==1
        bridgeview.loadMiwoUrl(video.go_url)
        mPresenter.initVideo(videoplayer,video)
    }

    fun  getImmersionBar():ImmersionBar{
        return mImmersionBar
    }

    override fun onDestroy() {
        destroymImmersionBar()
        super.onDestroy()
    }
    fun  destroymImmersionBar(){
        mImmersionBar!!.destroy()
    }
}
